package com.example.xt.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.xt.common.Constants;
import com.example.xt.common.Result;
import com.example.xt.entity.Dto.UserDTO;
import com.example.xt.entity.User;
import com.example.xt.service.IUserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2022-02-24
 */
@RestController
@RequestMapping("/user")
public class UserController {
       @Resource
       private IUserService userService;

    @PostMapping("/login")
    public Result login(@RequestBody UserDTO userDTO) {
        String username = userDTO.getUsername();
        String password = userDTO.getPassword();
        if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {//输入的用户名或密码为空
            return Result.error(Constants.CODE_400,"用户名或密码不能为空");
        }
        UserDTO dto= userService.login(userDTO);
        return Result.success(dto);
    }

       @PostMapping("/insert")
       public User save(@RequestBody User user) {
           String username = user.getUsername();
           String password = user.getPassword();
           String sion = user.getSion();
           String nickname = user.getNickname();

           if (username != null && password != null && sion != null && nickname != null) {
               if (userService.insert(user)) {
                   return Result.success(userService.saveOrUpdate(user));
               } else {
                   return Result.error(Constants.CODE_402, "数据库中已经存在相同的用户名");
               }
           } else {
               return Result.error(Constants.CODE_400, "用户名或密码不能为空");
           }
       }

    @PostMapping("/user/insert")
    public Result insert(@RequestBody User user) {
        return Result.success(userService.saveOrUpdate(user));
    }

       @DeleteMapping("/{id}")
       public Result delete(@PathVariable Integer id) {
              if(id!=1){return Result.success( userService.removeById(id));}
              else {return Result.error(Constants.CODE_300,"不可删除超级管理员");}
       }
        @GetMapping("/username/{username}")
        public Result person(@PathVariable String username) {
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("username",username);

            return Result.success(userService.getOne(queryWrapper));
        }

       @GetMapping
       public List<User> findAll() {
              return userService.list();
        }

        @PostMapping("/del/batch")
        public Result deleteBatch(@RequestBody List<Integer> ids) { // [1,2,3]

            if (ids.contains(1)) {
                return Result.error(Constants.CODE_300, "不可删除超级管理员");
            } else {
                return Result.success(userService.removeByIds(ids));
            }
        }


        @GetMapping("/{id}")
       public User findOne(@PathVariable Integer id) {
              return userService.getById(id);
        }

       @GetMapping("/page")
       public Page<User> findPage(@RequestParam Integer pageNum,
                                  @RequestParam Integer pageSize,
                                  @RequestParam (defaultValue = "") String username) {
               QueryWrapper<User> queryWrapper = new QueryWrapper<>();
               queryWrapper.orderByDesc("id");

               if (!"".equals(username)) {
                       queryWrapper.like("username", username);

               }
              return userService.page(new Page<>(pageNum, pageSize),queryWrapper);
        }

}

